import 'package:flutter/material.dart';
import 'package:flutterproject/page/mine/children/assets/childdren/formatter.dart';
import 'package:flutterproject/util/address_manager.dart';
import 'package:flutterproject/util/dialog_toast/dialog_util.dart';
import 'package:flutterproject/util/dialog_toast/toast_util.dart';
import 'package:flutterproject/util/extension/extension_util.dart';
import 'package:flutterproject/util/http/http_client.dart';
import 'package:flutterproject/util/http/http_exception.dart';
import 'package:flutterproject/util/margin_padding_util.dart';
import 'package:flutterproject/widget/app_bar_common.dart';
import 'package:flutterproject/widget/button_common.dart';
import 'package:flutterproject/widget/image_extended.dart';
import 'package:flutterproject/widget/text/text_common.dart';
import 'package:flutterproject/widget/text/text_field_common.dart';
import 'package:get/get.dart';

/// 酝积分充值
class YiDai extends StatefulWidget {
  final int? walletId;

  const YiDai({super.key, this.walletId});

  @override
  YiDaiState createState() => YiDaiState();
}

class YiDaiState extends State<YiDai> {
  final textController = TextEditingController();
  final focusNode = FocusNode();
  bool hasModify = false;

  @override
  void initState() {
    super.initState();
    textController.text = '0';
  }

  @override
  Widget build(BuildContext context) {
    return UnconstrainedBox(
      alignment: Alignment.topCenter,
      child: Container(
        margin: MarginPaddingUtil.symmetric(horizontal: 40, vertical: 64),
        padding: MarginPaddingUtil.fromLTRB(
            left: 30, top: 102, right: 30, bottom: 58),
        decoration: BoxDecoration(
          color: Color(0xFFF9B57B),
          borderRadius: BorderRadius.circular(40.radius)
        ),
        width: 640.width,
        height: 647.height,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              padding: MarginPaddingUtil.fromLTRB(
                  left: 20, top: 40, right: 0, bottom: 32),
              decoration: BoxDecoration(
                  color: Color(0xFFF3AC6F),
                  borderRadius: BorderRadius.circular(20.radius)),
              child: Row(
                children: [
                  Expanded(
                    child: Row(
                      children: [
                        TextCommon(
                          "从",
                          size: 28.sp,
                          color: Colors.white.withOpacity(0.5),
                        ),
                        Expanded(
                          child: TextCommon(
                            "众商通",
                            size: 28.sp,
                            color: Colors.white,
                            fontWeight: FontWeight.bold,
                            align: TextAlign.center,
                          ),
                        ),
                      ],
                    ),
                  ),
                  ImageCommon(
                    "/image/ic_change",
                    size: 48.width,
                  ),
                  Expanded(
                    child: Row(
                      children: [
                        42.widthBox,
                        TextCommon(
                          "至",
                          size: 28.sp,
                          color: Colors.white.withOpacity(0.5),
                        ),
                        Expanded(
                          child: TextCommon(
                            "聚星同辉",
                            size: 28.sp,
                            color: Colors.white,
                            fontWeight: FontWeight.bold,
                            align: TextAlign.center,
                          ),
                        ),
                      ],
                    ),
                  )
                ],
              ),
            ),
            50.heightBox,
            Container(
              padding: MarginPaddingUtil.fromLTRB(
                  left: 20, top: 24, right: 0, bottom: 24),
              decoration: BoxDecoration(
                  color: Color(0xFFF3AC6F),
                  borderRadius: BorderRadius.circular(20.radius)),
              child: Row(
                children: [
                  TextCommon(
                    "输入额度",
                    size: 28.sp,
                    color: Colors.white.withOpacity(0.5),
                  ),
                  Expanded(
                    child: TextFieldCommon(
                      align: TextAlign.right,
                      controller: textController,
                      height: 72.width,
                      size: 50.sp,
                      textColor: Colors.white,
                      padding: MarginPaddingUtil.symmetric(horizontal: 25),
                      keyboardType: TextInputType.number,
                      focusNode: focusNode,
                      inputFormatters: [InputFormatter()],
                      onChanged: (value) {
                        // if (value == "") {
                        //   (widget.controller ?? textController).text = "0";
                        // }
                      },
                    ),
                  ),
                ],
              ),
            ),
            76.heightBox,
            ButtonCommon(
              text: "确定充值",
              onTap: () => showPwdDialog(),
              width: 472.width,
              color: Colors.white,
              borderRadios: 30.radius,
              fontColor: Color(0xFFEDC88B),
              fontSize: 36.sp,
              fontWeight: FontWeight.bold,
              padding: MarginPaddingUtil.symmetric(vertical: 20),
            ),
          ],
        ),
      ),
    );
  }

  HttpClient client = Get.find<HttpClient>();

  /// 充值
  Future charge(String pwd) async {
    try {
      DialogUtil.showLoading();
      await client.post(
        AddressManager.charge,
        data: {
          "value": textController.text,
          "walletId": widget.walletId,
          "password": pwd,
        },
      );
      ToastUtil.showSuccess("充值成功!");
      Get.back();
      return true;
    } on HttpException catch (e) {
      ToastUtil.showFailed(e.message);
      return false;
    } finally {
      DialogUtil.dismiss();
    }
  }

  void showPwdDialog() {
    DialogUtil.showPwdDialog((value) {
      charge(value);
    });
  }
}
